﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Management;

namespace Atom.Utility
{
    public class EncryptDecrypt
    {
        public EncryptDecrypt()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        const string sKey = "SOFT-LIU";
        /// <summary>
        /// 文件加密
        /// </summary>
        /// <param name="sInputFilename"></param>
        /// <param name="sOutputFilename"></param>
        public static void EncryptFile(string sInputFilename, string sOutputFilename)
        {
            FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

            FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);

            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();
        }

        /// <summary>
        /// 文件解密
        /// </summary>
        /// <param name="sInputFilename"></param>
        /// <param name="sOutputFilename"></param>
        public static void DecryptFile(string sInputFilename, string sOutputFilename)
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

            ICryptoTransform desdecrypt = DES.CreateDecryptor();

            CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);

            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        }
        /// <summary>
        /// 对需要的加密的字符串进行加密
        /// </summary>
        /// <param name="originalString"></param>
        /// <returns></returns>
        public static string Encrypt(string originalString)
        {

            if (originalString.Trim() == string.Empty)
                return originalString;
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();//des进行加密

            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            ICryptoTransform desencrypt = DES.CreateEncryptor();

            MemoryStream ms = new MemoryStream();//存储加密后的数据
            CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write);
            byte[] data = Encoding.Unicode.GetBytes(originalString);//取到密码的字节流
            cs.Write(data, 0, data.Length);//进行加密
            cs.FlushFinalBlock();
            byte[] res = ms.ToArray();//取加密后的数据
            cs.Close();
            ms.Close();
            //return Encoding.Unicode.GetString(res);
            return Convert.ToBase64String(res);
        }
        //对需要解密的字符串进行解密
        public static string Decrypt(string encryptString)
        {

            if (encryptString.Trim() == string.Empty)
                return encryptString;
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            ICryptoTransform desencrypt = DES.CreateDecryptor();

            MemoryStream ms = new MemoryStream();//存储解密后的数据

            CryptoStream cs = new CryptoStream(ms, desencrypt, CryptoStreamMode.Write);
            //byte[] data = Encoding.Unicode.GetBytes(encryptString);//取到加密后的数据的字节流，如果是保存到文件，这里用Convert.FromBase64String(data)
            byte[] data = Convert.FromBase64String(encryptString);
            cs.Write(data, 0, data.Length);//解密数据
            try
            {
                cs.FlushFinalBlock();
            }
            catch
            {
                throw new Exception("数据不正确！");
            }
            byte[] res = ms.ToArray();
            cs.Close();
            ms.Close();
            return Encoding.Unicode.GetString(res);//返回解密后的数据，这里返回的数据应该和参数pwd的值相同。

        }

        //取CPU编号
        public static string GetCpuID()
        {
            try
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();

                String strCpuID = null;
                foreach (ManagementObject mo in moc)
                {
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return strCpuID;
            }
            catch
            {
                return "";
            }

        }//end method


        public static string GetMacInfo()
        {
            string info = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {

                if ((bool)mo["IPEnabled"] == true)
                {
                    info = mo["MacAddress"].ToString();
                    mo.Dispose();
                    break;
                }

            }
            return info;
        }
    }
}
